package com.adapter;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.R;
import com.dstats.SimpleWebView;
import com.entities.Reddit;
import com.fetch.ImageLoader;

public class RedditAdapter extends BaseExpandableListAdapter {
	private List<Reddit> redditList;
	private LayoutInflater mInflater;
	private Context activity;
	private static ImageLoader imageLoader;
	static class ViewHolder{
		TextView title;
		TextView voteCount;
		TextView commentsCount;
		TextView author;
		TextView timeAgo;
		TextView domain;
		Button goButton;
	}
	
	public RedditAdapter(Activity context, List<Reddit> reddit){
		mInflater = LayoutInflater.from(context);
		this.redditList = reddit;
		if(imageLoader==null)
		imageLoader = new ImageLoader(context);
		this.activity = context;
	}


	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
	        boolean isLastChild, View convertView, ViewGroup parent) {
		
		String selftext = redditList.get(groupPosition).getSelftext();
		String url = redditList.get(groupPosition).getUrl();
		TextView tv = new TextView(activity);
		if(selftext!=null && !selftext.isEmpty()){			
		tv.setText(selftext);
		tv.setTextColor(Color.BLACK);
		} else if(url.endsWith("jpg") || url.endsWith("png") || url.endsWith("gif")){
				
			ImageView img = new ImageView(activity);
			imageLoader.DisplayImage(url, img);
			return img;
			}
		else if (url.startsWith("http://www.youtube.com/")){
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(url));
			activity.startActivity(intent);
		}
		else {
			Intent intent = new Intent(activity, SimpleWebView.class);
			intent.putExtra("url", url);
			activity.startActivity(intent);
		}
		
		return tv;
	}
	@Override
	public int getChildrenCount(int arg0) {
		Reddit reddit = redditList.get(arg0);
		String url = reddit.getUrl();
		String selftext = reddit.getSelftext();
		if((selftext != null && !selftext.isEmpty()) || (url.endsWith("jpg") || url.endsWith("png") || url.endsWith("gif"))){
			return 1;
		}
		return 1;
	}
	@Override
	public Reddit getGroup(int arg0) {
		return redditList.get(arg0);
	}
	@Override
	public int getGroupCount() {
		return redditList.size();
	}
	@Override
	public long getGroupId(int arg0) {
		return 0;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
	        View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.reddit_row, null);
			
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.textViewRedditTitle);
			holder.voteCount = (TextView) convertView.findViewById(R.id.textViewRedditVoteCount);
			holder.author = (TextView) convertView.findViewById(R.id.textViewRedditAuthor);
			holder.commentsCount = (TextView) convertView.findViewById(R.id.textViewRedditQtyComments);
			holder.timeAgo = (TextView) convertView.findViewById(R.id.textViewRedditTimeAgo);
			holder.domain = (TextView) convertView.findViewById(R.id.textViewRedditDomain);
			holder.goButton = (Button) convertView.findViewById(R.id.buttonGo);
			convertView.setTag(holder);
			
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		final Reddit reddit = redditList.get(groupPosition);
		holder.title.setText(reddit.getTitle());
		holder.title.setTextColor(Color.BLACK);		   
		holder.author.setText("Author: " + reddit.getAuthor());
		holder.voteCount.setText(String.valueOf(reddit.getUps() - reddit.getDowns()) + "\n Points");
		holder.timeAgo.setText(String.valueOf(getTimeDiff(new Date(reddit.getCreated_utc()*1000))));
		holder.commentsCount.setText(String.valueOf(reddit.getNum_comments()) + " comments");
		holder.domain.setText(reddit.getDomain());
		holder.goButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(activity, SimpleWebView.class);
				intent.putExtra("url", reddit.getUrl());
				activity.startActivity(intent);
				
			}
		});
	
	
		return convertView;
	}
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}
	
	private String getTimeDiff(Date date) {
		Date curDate = new Date();
		
		long diff = curDate.getTime() - date.getTime();
		long mins = diff/60000;
		long hours = mins/60;
		long days = hours/24;
		long months = days/365/12;
		long years = months /12;
		Resources r = activity.getResources();
		String toPut;
		if(years>=1){
			if(years==1) toPut = String.valueOf(years) + " " + r.getString(R.string.year_ago);
			else toPut = String.valueOf(years) + " " + r.getString(R.string.years_ago);
		} else if (months>=1){
			if(months==1) toPut = String.valueOf(months) + " " + r.getString(R.string.month_ago); 
			else toPut = String.valueOf(months) + " " + r.getString(R.string.months_ago);
		} else if (days>=1) {
			if(days==1) toPut = String.valueOf(days) + " " + r.getString(R.string.day_ago);
			else toPut = String.valueOf(days) + " " + r.getString(R.string.days_ago);
		} else if (hours>=1){
			if(hours==1)  toPut = String.valueOf(hours) + " " + r.getString(R.string.hour_ago);
			else toPut = String.valueOf(hours) + " " + r.getString(R.string.hours_ago);
		} else {
			if(mins==1 || mins ==0) toPut = String.valueOf(mins) + " " + r.getString(R.string.minute_ago);
			else toPut = String.valueOf(mins) + " " + r.getString(R.string.minutes_ago);
		}
		return toPut;
	}

}
